import re

from django import forms
from django.core.exceptions import ValidationError

from luckin.common import (
    is_phone, is_md5
)


# 注册功能参数校验
class RegisterForm(forms.Form):
    '''
    需要验证的参数为 name(account),password,email,code
    '''
    account = forms.CharField(required=True)
    password = forms.CharField(required=True)
    re_password = forms.CharField(required=True)
    code = forms.CharField(required=True)

    def clean_account(self):
        account = self.cleaned_data.get("account").strip()
        if not is_phone(account):
            raise ValidationError("手机号格式错误")
        return account

    def clean_password(self):
        password = self.cleaned_data.get("password").strip()
        if not is_md5(password):
            raise ValidationError("密码格式错误")
        return password

    def clean_re_password(self):
        re_password = self.cleaned_data.get("re_password").strip()
        if not is_md5(re_password):
            raise ValidationError("密码格式错误")
        return re_password

    def clean_code(self):
        code = self.cleaned_data.get("code").strip()
        if not code.isdigit() or len(code) != 4:
            raise ValidationError("验证码格式错误")
        return code

    def clean(self):
        # 验证两次密码是否正确
        password = self.cleaned_data.get("password").strip()
        re_password = self.cleaned_data.get("re_password").strip()
        if password != re_password:
            raise ValidationError("两次密码不一致")
        return self.cleaned_data


# 登录参数校验
class LoginForm(forms.Form):
    account = forms.CharField(required=True)
    password = forms.CharField(required=True)
    code = forms.CharField(required=True)

    def clean_account(self):
        account = self.cleaned_data.get("account").strip()
        if not is_phone(account):
            raise ValidationError("手机号格式错误")
        return account

    def clean_password(self):
        password = self.cleaned_data.get("password").strip()
        if not is_md5(password):
            raise ValidationError("密码格式错误")
        return password

    def clean_code(self):
        code = self.cleaned_data.get("code").strip()
        if not code.isdigit() or len(code) != 4:
            raise ValidationError("验证码格式错误")
        return code


# 更新用户参数校验
class UpdateUserForm(forms.Form):
    nickname = forms.CharField(required=False)
    gender = forms.CharField(required=False)

    def clean_gender(self):
        # gender的值如果不合法，默认为1
        gender = self.cleaned_data.get("gender").strip()
        if not gender:
            return 1
        if gender not in ["1", "2", "3"]:
            return 1
        return gender

    def clean_nickname(self):
        # 检测nickname是否有非法字符
        nickname = self.cleaned_data.get("nickname").strip()
        # 不能有空格
        if not re.search(r"[\s=<>%-]", nickname):
            raise ValidationError("nickname 不能有特殊字符")
        return nickname

    def clean(self):
        nickname = self.cleaned_data.get("nickname").strip()
        gender = self.cleaned_data.get("gender").strip()
        if not nickname and not gender:
            raise ValidationError("参数非法")
        return self.cleaned_data


# 收获地址参数校验
class ShippingAddressForm(forms.Form):
    nickname = forms.CharField(required=True)
    gender = forms.CharField(required=True)
    phone = forms.CharField(required=True)
    address = forms.CharField(required=True)
    home_number = forms.CharField(required=True)
    address_tag = forms.CharField(required=True)
    is_default = forms.CharField(required=True)

    def clean_gender(self):
        # gender的值如果不合法，默认为1
        gender = self.cleaned_data.get("gender").strip()
        if not gender:
            return 1
        if gender not in ["1", "2"]:
            return 1
        return gender

    def clean_nickname(self):
        # 检测nickname是否有特殊字符
        nickname = self.cleaned_data.get("nickname").strip()
        # 不能有空格
        if re.search(r"[\s=<>%-]", nickname):
            raise ValidationError("nickname 不能有特殊字符")
        return nickname

    def clean_phone(self):
        # 检测手机号是否合法
        phone = self.cleaned_data.get("phone")
        if not is_phone(phone):
            raise ValidationError("手机号格式错误")
        return phone

    def clean_address(self):
        # 检测地址是否存在非法字符
        address = self.cleaned_data.get("address").strip()
        # 不能有空格
        if re.search(r"[=<>%]", address):
            raise ValidationError("address 不能有特殊字符")
        return address

    def clean_home_number(self):
        home_number = self.cleaned_data.get("home_number").strip()
        # 不能有空格
        if re.search(r"[=<>%]", home_number):
            raise ValidationError("home_number 不能有特殊字符")
        return home_number

    def clean_address_tag(self):
        # 默认为 1
        address_tag = self.cleaned_data.get("address_tag").strip()
        if not address_tag:
            return 1
        if address_tag not in ["1", "2", "3"]:
            return 1
        return address_tag

    def clean_is_default(self):
        is_default = self.cleaned_data.get("is_default").strip()
        if not is_default:
            return 2
        if is_default not in ["1", "2"]:
            return 2
        return is_default
